﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Security;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace IceCream.icemodel
{
    public class HttpClient
    {
        public static string url_start = "http://dibai.motonbackend.top/swoft/api/motong/";

        //public static string url_start = "https://" + IPorPortInfo.Instance.CoffeeIP + "/";
        private static HttpClient instance;
        private HttpClient() { }
        public static HttpClient Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new HttpClient();
                }
                return instance;
            }
        }
        public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {   // 总是接受  
            return true;
        }
        public string ServerGet(string url)
        {
            string result = string.Empty;
            try
            {
                url = url_start + url;
                HttpWebRequest request = null;
                request = WebRequest.Create(url) as HttpWebRequest;
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                request.ProtocolVersion = HttpVersion.Version11;
                // 这里设置了协议类型。
                ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; ;// SecurityProtocolType.Tls1.2;
                request.KeepAlive = false;
                ServicePointManager.CheckCertificateRevocationList = true;
                ServicePointManager.DefaultConnectionLimit = 100;
                ServicePointManager.Expect100Continue = false;

                request.Method = "GET"; //使用get方式发送数据
                request.ContentType = "application/json";
                request.Accept = "application/json";
                request.Referer = null;
                request.AllowAutoRedirect = true;

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream stream = response.GetResponseStream();
                using (StreamReader sr = new StreamReader(stream))
                {
                    result = sr.ReadToEnd();
                }
                //Console.Write(result);
                return result;
            }
            catch (Exception)
            {
                return result;
            }
        }
        private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
        {
            return true;
        }
        public string HttpPostRaw(string url, string data, bool istimeout = false)
        {
            string value = "";
            try
            {
                DateTime start = DateTime.Now;
                // Console.WriteLine("jiekou1  " + DateTime.Now.ToString("HHmmssfff"));
                string realurl = url_start + url;
                HttpWebRequest reqest = (HttpWebRequest)WebRequest.Create(realurl);
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                reqest.ProtocolVersion = HttpVersion.Version11;
                // 这里设置了协议类型。
                ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; ;// SecurityProtocolType.Tls1.2;
                reqest.KeepAlive = false;
                ServicePointManager.CheckCertificateRevocationList = true;
                ServicePointManager.DefaultConnectionLimit = 100;
                ServicePointManager.Expect100Continue = false;
                reqest.Method = "POST";
                reqest.ContentType = "application/json";
                Stream stream = reqest.GetRequestStream();
                byte[] bs = System.Text.Encoding.UTF8.GetBytes(data);
                stream.Write(bs, 0, bs.Length);
                stream.Flush();
                stream.Close();
                if (istimeout)
                    reqest.Timeout = 1000;
                ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
                HttpWebResponse response = (HttpWebResponse)reqest.GetResponse();
                DateTime end = DateTime.Now;
                TimeSpan ts = end - start;
                int ch = (int)ts.Milliseconds;//响应时间.(毫秒)  
                if (!istimeout)                              // Console.WriteLine("jiekou2  " + DateTime.Now.ToString("HHmmssfff"));
                    Console.WriteLine("jiekou " + end + " " + ts + " " + url);                                                     // Console.WriteLine(reqest.ReadWriteTimeout);
                                                                                                                                   //reqest.
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                value = sr.ReadToEnd();
                response.Close();
                return value;
            }
            catch (Exception ex)
            {
                // error = ex.ToString();
                return "";
            }
        }
        public void HttpPost(string Url, string postDataStr)
        {
            string realurl = url_start + Url;
            //CoffeeProductID ceshi = new CoffeeProductID();
            //ceshi.productId = postDataStr;
            //string json = JsonConvert.SerializeObject(ceshi);
            HttpPostRaw(realurl, postDataStr);

        }
    }
}
